package com.graceplayer.activity;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import com.graceplayer.data.Music;
import com.graceplayer.data.MusicList;
import com.graceplayer.model.PropertyBean;

import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 切换整个 APP 风格，使用 Material 的风格，安卓 5.0
 *
 * 对应 manifest 的 style 要是 AppCompat 的
 *
 * */
public class MainActivity extends Activity {

    /** Activity 的生命周期：
     *
     *  注意，不同情况有很多种，
     *   一般来说是：
     *      onCreate -> onStart   -> onResume -> onPause -> onStop -> onDestroy
     *      onPause  -> onReStart -> onResume
     *      finish   -> onDestroy
     *   切换横竖屏幕:
     *      onSaveInstanceState() -> onPause -> onStop -> onDestroy
     *      -> onCreate -> onStart -> onRestoreInstanceState()
     *      -> onResume -> onConfigurationChange()
     *      ...
     *
     *  */

    // 显示组件
    private ImageButton imgBtn_Previous;
    private ImageButton imgBtn_PlayOrPause;
    private ImageButton imgBtn_Stop;
    private ImageButton imgBtn_Next;
    private ListView list;
    private TextView text_Current;
    private TextView text_Duration;
    private TextView textView;
    private SeekBar seekBar;
    private RelativeLayout root_Layout;
    // 更新进度条的Handler
    private Handler seekBarHandler;
    // 当前歌曲的持续时间和当前位置，作用于进度条
    private int duration;
    private int time;
    // 进度条控制常量
    private static final int PROGRESS_INCREASE = 0;
    private static final int PROGRESS_PAUSE = 1;
    private static final int PROGRESS_RESET = 2;
    //歌曲列表对象
    private ArrayList<Music> musicArrayList;
    // 当前歌曲的序号，下标从0开始
    private int number = 0;
    // 播放状态
    private int status;
    // 广播接收器
    private StatusChangedReceiver receiver;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /** 代码优化 */
        /** 解决像 魅族 手机在 api 19 及其以下，没有菜单项的手机，切换风格的问题 */
        /** 下面这个是切换风格的按钮 */
        findViewById(R.id.setTheme).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                changeStyle(); /** 切换风格的代码函数 */
            }
        });

        findViews();
        registerListeners();
        initMusicList();
        initListView();
        checkMusicfile();
        duration = 0;
        time = 0;
        // 绑定广播接收器，可以接收广播
        bindStatusChangedReceiver();
        initSeekBarHandler();
        /** service 是 Android 四大组件之一 */
        /** 注意：
         *      service 并不是在 子线程中运行！
         *      IntentService 是在子线程中运行，面试经常问！
         *
         *  service 的启动方式：
         *      startService
         *      bindService
         *
         *  生命周期：
         *      startService: onCreate -> onStartCommand() -> onDestroy
         *      bindService :onCreate -> onBind ->onUnBind -> onDestroy
         *
         *  还一个：
         *      如果同时两种方式启动,onCreate 只会运行一次！
         * */
        startService(new Intent(this, MusicService.class)); /** 启动 service */
        status = MusicService.COMMAND_STOP;
    }

    /** 绑定广播接收器 */
    private void bindStatusChangedReceiver() {
        receiver = new StatusChangedReceiver();
        IntentFilter filter = new IntentFilter(
                MusicService.BROADCAST_MUSICSERVICE_UPDATE_STATUS);

        /** 广播是 android 四大组件之一，我一般用来进行进程间通讯，和 Service 结合使用 */
        /**
         * 注册方式：
         *
         *      静态注册，在 manifest 文件里面写；
         *      动态注册，在代码里面写；
         *
         *      区别：
         *          动态优先于静态；
         *          静态是跟随整个APP生存的，动态是当前的 Activity
         *          取消注册是，unregister
         * */
        registerReceiver(receiver, filter); /** 注册广播了 */
    }
    /** 获取显示组件 */
    private void findViews() {
        imgBtn_Previous = (ImageButton) findViewById(R.id.imageButton1);
        imgBtn_PlayOrPause = (ImageButton) findViewById(R.id.imageButton2);
        imgBtn_Stop = (ImageButton) findViewById(R.id.imageButton3);
        imgBtn_Next = (ImageButton) findViewById(R.id.imageButton4);
        list = (ListView) findViewById(R.id.listView1);
        seekBar = (SeekBar) findViewById(R.id.seekBar1);
        text_Current = (TextView) findViewById(R.id.textView1);
        text_Duration = (TextView) findViewById(R.id.textView2);
        textView = (TextView) findViewById(R.id.textView);
        root_Layout = (RelativeLayout) findViewById(R.id.relativeLayout1);
    }

    /** 为显示组件注册监听器 */
    private void registerListeners() {
        imgBtn_Previous.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                sendBroadcastOnCommand(MusicService.COMMAND_PREVIOUS);   /** 发送 上一首的 命令给 service */
            }
        });
        imgBtn_PlayOrPause.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                switch (status) {
                    case MusicService.STATUS_PLAYING:
                        sendBroadcastOnCommand(MusicService.COMMAND_PAUSE);  /** 发送 暂停的 命令给 service */
                        break;
                    case MusicService.STATUS_PAUSED:
                        sendBroadcastOnCommand(MusicService.COMMAND_RESUME); /** 发送 暂停后再次点击播放的 命令给 service */
                        break;
                    case MusicService.COMMAND_STOP:
                        sendBroadcastOnCommand(MusicService.COMMAND_PLAY);   /** 发送 播放的 命令给 service */
                    default:
                        break;
                }
            }
        });
        imgBtn_Stop.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                sendBroadcastOnCommand(MusicService.COMMAND_STOP);  /** 发送 停止播放 命令给 service */
            }
        });
        imgBtn_Next.setOnClickListener(new OnClickListener() {
            public void onClick(View view) {
                sendBroadcastOnCommand(MusicService.COMMAND_NEXT);  /** 发送 下一首的 命令给 service */
            }
        });
        list.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {
                number = position;
                sendBroadcastOnCommand(MusicService.COMMAND_PLAY);  /** 列表的每一项点击后发送播放的命令给，对应number的歌 service */
            }
        });
        /** 下面的是移动进度条的时候对应的事件监听 */
        /** stop 是停止态，seek_to 是移动到某一进度，playing 是正在播放态 */
        seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                if (status != MusicService.STATUS_STOPPED) {
                    time = seekBar.getProgress();
                    /** 更新文本 */
                    text_Current.setText(formatTime(time));
                    /** 发送广播给MusicService，执行跳转 */
                    sendBroadcastOnCommand(MusicService.COMMAND_SEEK_TO);
                }
                if (status == MusicService.STATUS_PLAYING){
                    /** 进度条恢复移动 */
                    seekBarHandler.sendEmptyMessageDelayed(PROGRESS_INCREASE,
                            1000);
                }
            }
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                /** 进度条暂停移动 */
                seekBarHandler.sendEmptyMessage(PROGRESS_PAUSE);
            }
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress,
                                          boolean fromUser) {
            }
        });
    }
    /**初始化音乐列表对象*/
    private void initMusicList() {
        musicArrayList = MusicList.getMusicList();
        //避免重复添加音乐
        if(musicArrayList.isEmpty()) /** isEmpty 判断数据列表里面有没有数据 */
        {
            /** 空的话就进入到里面获取数据 */
            Cursor mMusicCursor = this.getContentResolver().query(
                    MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
                    MediaStore.Audio.AudioColumns.TITLE);
            /** 获取音乐列表的 标题 对应 title*/
            int indexTitle = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.TITLE);
            /** 获取音乐列表的 艺术家 对应 artist ，就是说是谁唱的 */
            int indexArtist = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.ARTIST);
            /** 获取音乐列表的 总时长 对应 duration ，就是说是谁唱的 */
            int indexTotalTime = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DURATION);
            /** 获取音乐列表的 路径 对应 data ，就是说是谁唱的 */
            int indexPath = mMusicCursor.getColumnIndex(MediaStore.Audio.AudioColumns.DATA);

            /**通过mMusicCursor游标遍历数据库，并将Music类对象加载带ArrayList中*/
            for (mMusicCursor.moveToFirst(); !mMusicCursor.isAfterLast(); mMusicCursor
                    .moveToNext()) {

                String strTitle = mMusicCursor.getString(indexTitle); /** 标题 */
                String strArtist = mMusicCursor.getString(indexArtist); /** 艺术家 */
                String strTotoalTime = mMusicCursor.getString(indexTotalTime); /** 总时长 */
                String strPath = mMusicCursor.getString(indexPath); /** 路径 */

                /** 下面优化一下，中文用 UTF-8 编码一次 */
                if (strArtist.equals("<unknown>")) {
                    try {
                        strArtist = new String("无艺术家".getBytes(), "UTF-8");
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    }
                }
                Music music = new Music(strTitle, strArtist, strPath, strTotoalTime);
                musicArrayList.add(music);
            }
        }
    }
    /**设置适配器并初始化listView*/
    private void initListView() {
        List<Map<String, String>> list_map = new ArrayList<Map<String, String>>();
        HashMap<String, String> map;
        SimpleAdapter simpleAdapter;  /** 系统自带的基础数据适配器 */
        /** 数据遍历 */
        for (Music music : musicArrayList) {
            map = new HashMap<String, String>();
            map.put("musicName", music.getmusicName());
            map.put("musicArtist", music.getmusicArtist());
            list_map.add(map);
        }

        String[] from = new String[] { "musicName", "musicArtist" };
        int[] to = { R.id.listview_tv_title_item, R.id.listview_tv_artist_item };

        simpleAdapter = new SimpleAdapter(this, list_map, R.layout.listview,from, to);
        list.setAdapter(simpleAdapter); /** 绑定数据源 */
    }

    /**如果列表没有歌曲，则播放按钮不可用，并提醒用户*/
    private void checkMusicfile()
    {
        if (musicArrayList.isEmpty()) {
            imgBtn_Next.setEnabled(false);
            imgBtn_PlayOrPause.setEnabled(false);
            imgBtn_Previous.setEnabled(false);
            imgBtn_Stop.setEnabled(false);
            Toast.makeText(getApplicationContext(), "当前没有歌曲文件",Toast.LENGTH_SHORT).show();
        } else {
            imgBtn_Next.setEnabled(true);
            imgBtn_PlayOrPause.setEnabled(true);
            imgBtn_Previous.setEnabled(true);
            imgBtn_Stop.setEnabled(true);
        }
    }
    /** 格式化：毫秒 -> "mm:ss" */
    private String formatTime(int msec) {
        int minute = (msec / 1000) / 60;
        int second = (msec / 1000) % 60;
        String minuteString;
        String secondString;
        if (minute < 10) {
            minuteString = "0" + minute;
        } else {
            minuteString = "" + minute;
        }
        if (second < 10) {
            secondString = "0" + second;
        } else {
            secondString = "" + second;
        }
        return minuteString + ":" + secondString;
    }
    private void initSeekBarHandler() {
        seekBarHandler = new Handler() {
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                switch (msg.what) {
                    case PROGRESS_INCREASE:
                        if (seekBar.getProgress() < duration) {
                            // 进度条前进1秒
                            seekBar.setProgress(time);
                            seekBarHandler.sendEmptyMessageDelayed(PROGRESS_INCREASE, 1000);
                            // 修改显示当前进度的文本
                            text_Current.setText(formatTime(time));
                            time += 1000;
                        }
                        break;
                    case PROGRESS_PAUSE:
                        seekBarHandler.removeMessages(PROGRESS_INCREASE);
                        break;
                    case PROGRESS_RESET:
                        // 重置进度条界面
                        seekBarHandler.removeMessages(PROGRESS_INCREASE);
                        seekBar.setProgress(0);
                        text_Current.setText("00:00");
                        break;
                }
            }
        };
    }
    /** 发送命令，控制音乐播放。参数定义在MusicService类中 */
    private void sendBroadcastOnCommand(int command) {

        Intent intent = new Intent(MusicService.BROADCAST_MUSICSERVICE_CONTROL);
        intent.putExtra("command", command);
        // 根据不同命令，封装不同的数据
        switch (command) {
            case MusicService.COMMAND_PLAY:
                intent.putExtra("number", number);
                break;
            case MusicService.COMMAND_SEEK_TO:
                intent.putExtra("time", time);
                break;
            case MusicService.COMMAND_PREVIOUS:
            case MusicService.COMMAND_NEXT:
            case MusicService.COMMAND_PAUSE:
            case MusicService.COMMAND_STOP:
            case MusicService.COMMAND_RESUME:
            default:
                break;
        }
        sendBroadcast(intent);
    }
    /** 内部类，用于播放器状态更新的接收广播 */
    class StatusChangedReceiver extends BroadcastReceiver {

        /** 接受到广播的时候就会调用这个函数 */
        @Override
        public void onReceive(Context context, Intent intent) {
            // 获取播放器状态
            status = intent.getIntExtra("status", -1);
            String musicName = intent.getStringExtra("musicName");
            String musicArtist = intent.getStringExtra("musicArtist");

            /** 开关语句对应不同状态的对应操作 */

            /** STATUS_PLAYING 播放态，STATUS_PAUSED 暂停态，STATUS_STOPPED 停止态，STATUS_COMPLETED 播放完成态 */

            switch (status) {
                case MusicService.STATUS_PLAYING:
                    seekBarHandler.removeMessages(PROGRESS_INCREASE);
                    time = intent.getIntExtra("time", 0);
                    duration = intent.getIntExtra("duration", 0);
                    number = intent.getIntExtra("number", number);
                    list.setSelection(number);
                    seekBar.setProgress(time);
                    seekBar.setMax(duration);
                    seekBarHandler.sendEmptyMessageDelayed(PROGRESS_INCREASE, 1000);
                    text_Duration.setText(formatTime(duration));
                    imgBtn_PlayOrPause.setBackgroundResource(R.drawable.pause);
                    // 设置textView文字，提示正在播放的歌曲
                    textView.setText(musicArtist + " - " + musicName);
                    break;
                case MusicService.STATUS_PAUSED:
                    seekBarHandler.sendEmptyMessage(PROGRESS_PAUSE);
                    imgBtn_PlayOrPause.setBackgroundResource(R.drawable.play);
                    // 设置textView文字，提示已经播放的歌曲
                    textView.setText(musicArtist + " - " + musicName);
                    break;
                case MusicService.STATUS_STOPPED:
                    time = 0;
                    duration = 0;
                    text_Current.setText(formatTime(time));
                    text_Duration.setText(formatTime(duration));
                    seekBarHandler.sendEmptyMessage(PROGRESS_RESET);
                    textView.setText("");
                    imgBtn_PlayOrPause.setBackgroundResource(R.drawable.play);
                    break;
                case MusicService.STATUS_COMPLETED:
                    number = intent.getIntExtra("number", 0);
                    if(number == MusicList.getMusicList().size()-1)
                        sendBroadcastOnCommand(MusicService.STATUS_STOPPED);
                    else
                        sendBroadcastOnCommand(MusicService.COMMAND_NEXT);
                    seekBarHandler.sendEmptyMessage(PROGRESS_RESET);
                    textView.setText("");
                    imgBtn_PlayOrPause.setBackgroundResource(R.drawable.play);
                    break;
                default:
                    break;
            }
        }
    }
    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        if (status == MusicService.STATUS_STOPPED) {
            stopService(new Intent(this, MusicService.class));
        }
        super.onDestroy();
    }
    /** 设置Activity的主题，包括修改背景图片等 */
    private void setTheme(String theme) {
        if ("彩色".equals(theme)) {
            Log.d("zzzzz","风格名称是----------- "+theme);
            root_Layout.setBackgroundResource(R.drawable.bg_color);
        } else if ("花朵".equals(theme)) {
            root_Layout.setBackgroundResource(R.drawable.bg_digit_flower);
        } else if ("群山".equals(theme)) {
            root_Layout.setBackgroundResource(R.drawable.bg_mountain);
        } else if ("小狗".equals(theme)) {
            root_Layout.setBackgroundResource(R.drawable.bg_running_dog);
        } else if ("冰雪".equals(theme)) {
            root_Layout.setBackgroundResource(R.drawable.bg_snow);
        } else if ("女孩".equals(theme)) {
            root_Layout.setBackgroundResource(R.drawable.bg_music_girl);
        } else if ("朦胧".equals(theme)) {
            root_Layout.setBackgroundResource(R.drawable.bg_blur);
        }
    }

    /** 创建菜单 */
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.main, menu);
        return super.onCreateOptionsMenu(menu);
    }
    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        sendBroadcastOnCommand(MusicService.COMMAND_CHECK_IS_PLAYING);
        PropertyBean property = new PropertyBean(MainActivity.this);
        String theme = property.getTheme();
        // 设置Activity的主题
        setTheme(theme);
    }

    /** 处理菜单点击事件 */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.menu_theme:
                changeStyle();
                break;
            case R.id.menu_about:
                // 显示文本对话框
                new AlertDialog.Builder(MainActivity.this).setTitle("GracePlayer")
                        .setMessage(R.string.about2).show();
                break;
            case R.id.menu_quit:
                //退出程序
                new AlertDialog.Builder(MainActivity.this).setTitle("提示")
                        .setMessage(R.string.quit_message).setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                        System.exit(0);
                    }
                }).setNegativeButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface arg0, int arg1) {
                        // TODO Auto-generated method stub
                    }
                }).show();
                break;
        }
        return super.onOptionsItemSelected(item);
    }

    /** 切换风格 */
    private void changeStyle(){
        /** 切换风格 */
        try {
            new AlertDialog.Builder(this)
                    .setTitle(new String("请选择主题".getBytes(),"UTF-8"))
                    .setItems(R.array.theme,
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int which) {
                                    // 获取在array.xml中定义的主题名称
                                    String theme = PropertyBean.THEMES[which];
                                    Log.d("zzzzz","风格名称是 "+theme);
                                    // 设置Activity的主题
                                    setTheme(theme);
                                    // 保存选择的主题
                                    PropertyBean property = new PropertyBean(MainActivity.this);
                                    property.setAndSaveTheme(theme);
                                }
                            }).show();
        } catch (UnsupportedEncodingException e) { /** 捕获下转码时可能会引发的异常 */
            e.printStackTrace();
        }
    }

    // 媒体播放类
    private MediaPlayer player = new MediaPlayer();


    /** 读取音乐文件 */
    private void load(int number) {
        try {
            player.reset();
            player.setDataSource(MusicList.getMusicList().get(number).getmusicPath());
            player.prepare();
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    /** 播放音乐 */
    private void play(int number) {
        // 停止当前播放
        if (player != null && player.isPlaying()) {
            player.stop();
        }
        load(number);
        player.start();
    }
    /** 暂停音乐 */
    private void pause() {
        if (player.isPlaying()) {
            player.pause();
        }
    }
    /** 停止播放 */
    private void stop() {
        player.stop();
    }
    /** 恢复播放（暂停之后） */
    private void resume() {
        player.start();
    }
    /** 重新播放（播放完成之后） */
    private void replay() {
        player.start();
    }

    /** 选择下一曲 */
    private void moveNumberToNext() {
        // 判断是否到达了列表底端
        if ((number ) == MusicList.getMusicList().size()-1) {
            Toast.makeText(MainActivity.this,MainActivity.this.getString(R.string.tip_reach_bottom),Toast.LENGTH_SHORT).show();
        } else {
            ++number;
            play(number);
        }
    }

    /** 选择上一曲 */
    private void moveNumberToPrevious() {
        // 判断是否到达了列表顶端
        if (number == 0) {
            Toast.makeText(MainActivity.this,MainActivity.this.getString(R.string.tip_reach_top),Toast.LENGTH_SHORT).show();
        } else {
            --number;
            play(number);
        }
    }
}
